//
//  SaleReportViewController.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/2.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "SaleReportViewController.h"
#import "VipCardReportHeadView.h"
#import "SaleReportTableViewCell.h"
#import "VipCardReportSectionHeadView.h"
#import "VipCardReportSectionFootView.h"
#import "VipCardReportFilterView.h"
#import "UserAccount.h"
@interface SaleReportViewController ()<UITableViewDataSource,UITableViewDelegate,VipCardReportHeadViewDelegate>

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,weak)VipCardReportHeadView *headView;

@property(nonatomic,strong)VipCardReportSectionHeadView *headSView;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,strong)NSMutableDictionary *filtDict;

@end

@implementation SaleReportViewController

-(NSMutableDictionary *)filtDict
{
    
    if (!_filtDict) {
        _filtDict = [NSMutableDictionary dictionary];
    }
    return _filtDict;
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"销售员报表";
    
    [self createSubView];
    
    self.view.backgroundColor = BackGroundColor;
    
    [self loadData];
}

-(void)createSubView{
    
    
    VipCardReportHeadView *headView = [[VipCardReportHeadView alloc] init];
    headView.vipCardReportHeadViewDelegate = self;
    [self.view addSubview:headView];
    
    
    [headView.fitBtn addTarget:self action:@selector(filterBtnClick)];
    
    self.headView = headView;
    [headView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.width.mas_equalTo(self.view);
    }];
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[SaleReportTableViewCell class] forCellReuseIdentifier:@"SaleReportTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = CSClearColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(headView.mas_bottom).offset(10);
        
        make.left.width.bottom.mas_equalTo(self.view);
    }];
    
}

-(void)loadData{
    
    //GET_SalerReport
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"ShopID"] forKey:@"ShopID"];
    
    [params setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"UserID"] forKey:@"TeacherID"];
    
    [params setObject:self.headView.timeLabel1.text forKey:@"beginDate"];
    
    [params setObject:self.headView.timeLabel2.text forKey:@"endDate"];
    
//    [params setDictionary:self.filtDict];
    
    NSLog(@"%@==%@",self.filtDict,params);
    
    [HTTPTool GET:GET_GetCourseOrderDetailReport_ByTearch parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count == 0) {
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView hideInView:self.view];
                  }
                  
                  self.dataSource = [NSMutableArray arrayWithArray:resultArray];
                  
                  int totalCount = 0;
                  int totalFee = 0;
                  int totalBonus = 0;
                  
                  for (NSDictionary *dict in self.dataSource) {
                      
                      totalCount += [[dict objectForKey:@"TradeCount"] intValue];
                      
                      totalFee += [[dict objectForKey:@"TotalFee"] intValue];//销售总额
                      
                      totalBonus += [[dict objectForKey:@"FactFee"] intValue];//实收总额
                  }
                  
                  
                  if (self.dataSource.count > 0) {
                      self.headSView.countLabel1.text = [NSString stringWithFormat:@"%ld",self.dataSource.count];
                      
                      self.headSView.netReceiptLabel1.text = [NSString stringWithFormat:@"%ld",totalFee];;
                      
                      self.headSView.receiveLabel1.text = [NSString stringWithFormat:@"%f",totalBonus - totalFee];;
                      
                      self.headSView.typeLabel1.text = [NSString stringWithFormat:@"%ld",(totalBonus - totalFee)/totalBonus];
                  }
                  
                  [self.tableView reloadData];
                  
              }
              
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
    
    if (self.dataSource.count == 0) {
        
        self.noDataView = [NoDataView shareInstance];
        
        [self.noDataView showInView:self.view];
    }else
    {
        self.noDataView = [NoDataView shareInstance];
        
        [self.noDataView hideInView:self.view];
        
    }
    
}


#pragma mark 过滤
-(void)filterBtnClick{
    
    __weak typeof(self)weakSelf = self;
    
    VipCardReportFilterView *vipFilterView = [[VipCardReportFilterView alloc] init];
    vipFilterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    vipFilterView.filterBlock = ^(NSMutableDictionary *filterDict) {
        
        weakSelf.filtDict = filterDict;
        
        [weakSelf loadData];
    };
    
    [[UIApplication sharedApplication].keyWindow addSubview:vipFilterView];
}

#pragma mark UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    return 10;
    return self.dataSource.count ;
}

#pragma mark UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    VipCardReportSectionHeadView *headView = [[VipCardReportSectionHeadView alloc] init];
    
    //读取保存的数据
    UserAccount *acount = [UserAccount account];
    if (acount) {
        [headView.iconImage sd_setImageWithURL:[Helper imageUrlStringToChinese:acount.Pic imageType:nil] placeholderImage:PlaceHold];
        headView.nameLabel.text = acount.Name;
        headView.sexImage.image = [acount.Sex integerValue] == 0?[UIImage imageNamed:@"icon_man"]:[UIImage imageNamed:@"icon_women"];
        headView.telLabel.text = acount.CellPhone;
    }
    
    
    self.headSView = headView;

    
    
    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
//    self.footView = [[VipCardReportSectionFootView alloc] init];
//
//    int totalCount = 0;
//    int totalFee = 0;
//    int totalBonus = 0;
//
//    for (NSDictionary *dict in self.dataSource) {
//
//        totalCount += [[dict objectForKey:@"TradeCount"] intValue];
//
//        totalFee += [[dict objectForKey:@"TotalFee"] intValue];
//
//        totalBonus += [[dict objectForKey:@"TotalBonus"] intValue];
//    }
//
//    self.footView.countLabel.text = [NSString stringWithFormat:@"%d笔",totalCount];
//
//    self.footView.netReceiptLabel.text = [NSString stringWithFormat:@"￥%d",totalFee];
//
//    self.footView.receiveLabel.text = [NSString stringWithFormat:@"￥%d",totalBonus];
//
    return [[UIView alloc]initWithFrame:CGRectZero];;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    return 200;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SaleReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SaleReportTableViewCell"];
    
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
    
    
    NSInteger totalFee = [[dict objectForKey:@"TotalFee"] intValue];//销售总额
    
    NSInteger totalBonus = [[dict objectForKey:@"FactFee"] intValue];//实收总额
    
    cell.typeLabel.text = [NSString stringWithFormat:@"%f",(totalBonus - totalFee)/totalBonus];
    
    cell.countLabel.text= [NSString stringWithFormat:@"1笔"];
    
    cell.netReceiptLabel.text= [NSString stringWithFormat:@"￥%@",[dict objectForKey:@"TotalFee"]];
    
    cell.receiveLabel.text= [NSString stringWithFormat:@"￥%ld",totalBonus - totalFee];;
    
    if (indexPath.row % 2 == 0) {
        
        cell.backgroundColor = CSColorFromRGB(0xf9f9f9);
    }
    else{
        
        cell.backgroundColor = CSWhiteColor;
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
}


#pragma mark VipCardReportHeadViewDelegate
-(void)operateClick{
    
    [self loadData];
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
    }
    
    return _dataSource;
}

@end
